package org.jeecg.modules.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.blog.domain.DTO.BlogArticleDTO;
import org.jeecg.modules.blog.domain.entity.BlogArticle;
import org.jeecg.modules.blog.domain.entity.BlogArticlePraise;
import org.jeecg.modules.blog.domain.entity.BlogComment;
import org.jeecg.modules.blog.domain.param.BlogArticleParam;
import org.jeecg.modules.blog.service.IBlogArticlePraiseService;
import org.jeecg.modules.blog.service.IBlogArticleService;
import org.jeecg.modules.blog.service.IBlogCommentService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-04-30-22:20
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/blog/article")
@Api(tags = "博客")
public class BlogArticleController {

    @Autowired
    private IBlogArticleService articleService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IBlogArticlePraiseService articlePraiseService;

    @Autowired
    private IBlogCommentService blogCommentService;

    @RequestMapping("list")
    @ApiOperation(value = "list")
    public Object list(BlogArticle blogArticle , @RequestParam(value = "pageNo" , defaultValue = "1")
                       Integer pageNo , @RequestParam(value = "pageSize" , defaultValue = "10")Integer pageSize,
                       HttpServletRequest request
                       ){
        System.out.println("過來了");
        QueryWrapper<BlogArticle> queryWrapper = QueryGenerator.initQueryWrapper(blogArticle , request.getParameterMap());
        Page<BlogArticle> page = new Page<>(pageNo,pageSize);
        queryWrapper.lambda().orderBy( true,true, BlogArticle::getCreateTime);
        IPage pageList = articleService.page(page , queryWrapper);

        List<BlogArticle> records = pageList.getRecords();

        List<BlogArticleDTO> transform = Lists.transform(records, entity -> {
            //获取头像
            SysUser byId = userService.getById(entity.getUserId());
            //获取点赞数
            LambdaQueryWrapper<BlogArticlePraise> lambdaQueryWrapper  = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BlogArticlePraise::getArticleId , entity.getId());
            int praiseCount = articlePraiseService.count(lambdaQueryWrapper);

            //获取评论数
            LambdaQueryWrapper<BlogComment> commentLambdaQueryWrapper  = new LambdaQueryWrapper<>();
            commentLambdaQueryWrapper.eq(BlogComment::getArticleId , entity.getId());
            int commentCount = blogCommentService.count(commentLambdaQueryWrapper);


            BlogArticleDTO blogArticleDTO = new BlogArticleDTO();
            BeanUtils.copyProperties(entity, blogArticleDTO);
            if (byId.getAvatar()!=null){
                blogArticleDTO.setAvatar(byId.getAvatar());
            }
            blogArticleDTO.setPraiseCount(praiseCount);
            blogArticleDTO.setCommentCount(commentCount);
            return blogArticleDTO;
        });

        pageList.setRecords(transform);

        return Result.OK(pageList);
    }

    @GetMapping("/getOne/{id}")
    public Object getOne( @PathVariable String id){
        System.out.println("id：：："+id);
        return articleService.getById(id);
    }

    @RequestMapping(value = "/add" ,method = {RequestMethod.POST ,RequestMethod.PUT})
    public Object add(@RequestBody BlogArticleParam blogArticleParam){
        BlogArticle blogArticle = new BlogArticle();
        BeanUtils.copyProperties(blogArticleParam , blogArticle);
        LoginUser loginUser =(LoginUser) SecurityUtils.getSubject().getPrincipal();
        blogArticle.setUsername(loginUser.getUsername());
        blogArticle.setUserId(loginUser.getId());
        System.out.println(blogArticle);
        articleService.save(blogArticle);
        return Result.OK("添加成功");
    }

}
